package com.river.boot.biz.controller;

import com.river.boot.biz.service.ShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.river.boot.biz.model.form.ShopForm;
import com.river.boot.biz.model.query.ShopQuery;
import com.river.boot.biz.model.vo.ShopVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.river.boot.common.result.PageResult;
import com.river.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 店铺管理前端控制层
 *
 * @author JiangCheng Xiang
 * @since 2025-04-24 11:56
 */
@Tag(name = "12.店铺接口")
@RestController
@RequestMapping("/api/v1/shops")
@RequiredArgsConstructor
public class ShopController  {

    private final ShopService shopService;

    @Operation(summary = "店铺分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('biz:shop:query')")
    public PageResult<ShopVO> getShopPage(ShopQuery queryParams ) {
        IPage<ShopVO> result = shopService.getShopPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增店铺")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('biz:shop:add')")
    public Result<Void> saveShop(@RequestBody @Valid ShopForm formData ) {
        boolean result = shopService.saveShop(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取店铺表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('biz:shop:edit')")
    public Result<ShopForm> getShopForm(
        @Parameter(description = "店铺管理ID") @PathVariable Long id
    ) {
        ShopForm formData = shopService.getShopFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改店铺")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('biz:shop:edit')")
    public Result<Void> updateShop(
            @Parameter(description = "店铺管理ID") @PathVariable Long id,
            @RequestBody @Validated ShopForm formData
    ) {
        boolean result = shopService.updateShop(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除店铺")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('biz:shop:delete')")
    public Result<Void> deleteShops(
        @Parameter(description = "店铺管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = shopService.deleteShops(ids);
        return Result.judge(result);
    }
}
